<!DOCTYPE html>
<html>
    
<head>
    <meta charset="utf-8">
    
    <title>一文彻底了解Android自定义控件2 | ApocalypseBlog</title>
    <canvas id="header_canvas"style="position:absolute;bottom:0"></canvas> 
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    

    

    

    

    
<link rel="stylesheet" href="/dist/build.css?v=1.14.0.css">

    <script src="/javascripts/bubble.js"></script>
    <script>
        window.isPost = true
        window.aomori = {
            
            
        }
        window.aomori_logo_typed_animated = false
        window.aomori_search_algolia = false

    </script>
<script>
    ((window.gitter = {}).chat = {}).options = {
      room: 'ApocalypseBlog/Apocalypse'
    };
  </script>
  <script src="https://sidecar.gitter.im/dist/sidecar.v1.js" async defer></script>
<meta name="generator" content="Hexo 5.3.0"></head>
<!--DaoVoice服务http://blog.daovoice.io/-->

    <script>(function (i, s, o, g, r, a, m) {
        i['DaoVoiceObject'] = r;
        i[r] = i[r] ||
          function () {
            (i[r].q = i[r].q || []).push(arguments);
          };
        i[r].l = 1 * new Date();
        a = s.createElement(o);
        m = s.getElementsByTagName(o)[0];
        a.async = 1;
        a.src = g;
        a.charset = 'utf-8';
        m.parentNode.insertBefore(a, m);
      })(window, document, 'script', ('https:' === document.location.protocol ? 'https:' : 'http:') + "//widget.daovoice.io/widget/b00f5052.js", 'daovoice');
      daovoice('init', {
        app_id: "b00f5052",
      });
      daovoice('update');
    </script>
  
<body>

    <div class="container">
    <header class="header">
        <div class="header-type">
            
            <div class="header-type-inner">
                
                    <a class="header-type-title" href="/">ApocalypseBlog</a>
                
    
                
            </div>
        </div>
        <div class="header-menu">
            <div class="header-menu-inner">
                
            </div>
            <div class="header-menu-social">
                
            </div>
        </div>

        <div class="header-menu-mobile">
            <div class="header-menu-mobile-inner" id="mobile-menu-open">
                <i class="icon icon-menu"></i>
            </div>
        </div>
    </header>

    <div class="header-menu-mobile-menu">
        <div class="header-menu-mobile-menu-bg"></div>
        <div class="header-menu-mobile-menu-wrap">
            <div class="header-menu-mobile-menu-inner">
                <div class="header-menu-mobile-menu-close" id="mobile-menu-close">
                    <i class="icon icon-cross"></i>
                </div>
                <div class="header-menu-mobile-menu-list">
                    
                </div>
            </div>
        </div>
    </div>

</div>

    <div class="container">
        <div class="main">
            <section class="inner">
                <section class="inner-main">
                    <div class="post">
    <article id="post-ckk6o6auu000c7knybxm4314d" class="article article-type-post" itemscope
    itemprop="blogPost">

    <div class="article-inner">

        
          
        
        
        

        
        <header class="article-header">
            
  
    <h1 class="article-title" itemprop="name">
      一文彻底了解Android自定义控件2
    </h1>
  

        </header>
        

        <div class="article-more-info article-more-info-post hairline">

            <div class="article-date">
  <time datetime="2020-06-11T21:37:19.000Z" itemprop="datePublished">2020-06-12</time>
</div>

            

            

            

        </div>

        <div class="article-entry post-inner-html hairline" itemprop="articleBody">
            <p>经过前面对Android自定义控件初步了解以后，我们接下来就在前面的基础上更加深入了解所谓的Android自定义控件！</p>
<h3 id="AndroidView体系"><a href="#AndroidView体系" class="headerlink" title="AndroidView体系"></a>AndroidView体系</h3><p>在学习自定义控件之前，我们先了解一下Android的View体系</p>
<a id="more"></a>

<p>打开Android Studio –&gt; ctrl+N –&gt; 输入View</p>
<p><img src="http://user-gold-cdn.xitu.io/2020/6/6/172877a33009dc05?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img"></p>
<p>这样子，我们就找到了View这个类了！</p>
<h3 id="View的继承关系"><a href="#View的继承关系" class="headerlink" title="View的继承关系"></a>View的继承关系</h3><p>进到View这个类里， 选中类名，按ctrl+H，显示类的继承关系：</p>
<p><img src="http://user-gold-cdn.xitu.io/2020/6/6/172877ae65a73b28?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img"></p>
<p>我们可以看到一些熟悉的控件，比如说<code>ProgressBar</code>,<code>ImageView</code>,<code>TextView</code></p>
<p>所以我们得到如下的继承关系:</p>
<p><img src="http://user-gold-cdn.xitu.io/2020/6/6/172877b2585e8c53?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img"></p>
<p>继续打开<code>ViewGroup</code></p>
<p><img src="http://user-gold-cdn.xitu.io/2020/6/6/172877b6011de279?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img"></p>
<p>所以我们是又可以得到以下这张关系转图：</p>
<p><img src="http://user-gold-cdn.xitu.io/2020/6/6/172877b8beacb583?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img"></p>
<h3 id="View和ViewGrop的区别"><a href="#View和ViewGrop的区别" class="headerlink" title="View和ViewGrop的区别"></a>View和ViewGrop的区别</h3><p>先看ViewGroup，就是View组的意思是吧！也就是说，ViewGrop里面有很多子View，用于管理/摆放子View的View就叫ViewGroup。</p>
<p>什么是View呢？严格上来说，都是View，为什么呢？有了前面的定义，ViewGroup里面的全是View，因为你是View-Group呀！而ViewGrop里也可以方ViewGrop，所以说，ViewGroup也可以认为是View。比如说LinearLayout里面还可以放LinearLayout吧！</p>
<p>但是通常来说，我们说自定义View指的是定那些需要自己绘制的控件，重点是绘制，比如说时钟呀，波浪效果这些…View里面不再摆放其他的子View，我们把这种称为View</p>
<p>而ViewGroup，则是用于控制View的摆放，比如说LinearLayout，让子View成线性摆放，RelativeLayout让子View以相对的位置进行摆放..</p>
<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p>对于Android的View体系，了解继承关系，知道怎么区分是View，什么是ViewGroup即可。View注重的是绘制内容，ViewGropu注重子View的摆放。</p>
<p>有了这些基础知识，我们后面写自定义控件就可以步骤化了。第一步就是要判断你写的这个控件属于ViewGrop还是View.</p>

        </div>

    </div>

    

    

    

    

    

    
<nav class="article-nav">
  
    <a href="/2020/06/12/%E4%B8%80%E6%96%87%E5%BD%BB%E5%BA%95%E4%BA%86%E8%A7%A3Android%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8E%A7%E4%BB%B63/" id="article-nav-newer" class="article-nav-link-wrap">
      <div class="article-nav-caption">下一篇</div>
      <div class="article-nav-title">
        
          一文彻底了解Android自定义控件3
        
      </div>
    </a>
  
  
    <a href="/2020/06/12/%E4%B8%80%E6%96%87%E5%BD%BB%E5%BA%95%E4%BA%86%E8%A7%A3Android%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8E%A7%E4%BB%B61/" id="article-nav-older" class="article-nav-link-wrap">
      <div class="article-nav-caption">上一篇</div>
      <div class="article-nav-title">一文彻底了解Android自定义控件1</div>
    </a>
  
</nav>


    <section class="share">
        <div class="share-title">分享</div>
        <a class="share-item" target="_blank"
            href="https://twitter.com/share?text=一文彻底了解Android自定义控件2 - ApocalypseBlog&url=http://example.com/2020/06/12/%E4%B8%80%E6%96%87%E5%BD%BB%E5%BA%95%E4%BA%86%E8%A7%A3Android%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8E%A7%E4%BB%B62/">
            <box-icon type='logo' name='twitter'></box-icon>
        </a>
        <a class="share-item" target="_blank"
            href="https://www.facebook.com/sharer.php?title=一文彻底了解Android自定义控件2 - ApocalypseBlog&u=http://example.com/2020/06/12/%E4%B8%80%E6%96%87%E5%BD%BB%E5%BA%95%E4%BA%86%E8%A7%A3Android%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8E%A7%E4%BB%B62/">
            <box-icon name='facebook-square' type='logo' ></box-icon>
        </a>
        <!-- <a class="share-item" target="_blank"
            href="https://service.weibo.com/share/share.php?title=一文彻底了解Android自定义控件2 - ApocalypseBlog&url=http://example.com/2020/06/12/%E4%B8%80%E6%96%87%E5%BD%BB%E5%BA%95%E4%BA%86%E8%A7%A3Android%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8E%A7%E4%BB%B62/&pic=">
            <div class="n-icon n-icon-weibo"></div>
        </a> -->
    </section>

</article>









</div>
                </section>
            </section>

             
            <aside class="sidebar">
            
                
            </aside>
        </div>
    </div>

    <footer class="footer">
    <div class="footer-wave">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320"><path fill="#3c4859" fill-opacity="1" d="M0,160L60,181.3C120,203,240,245,360,240C480,235,600,181,720,186.7C840,192,960,256,1080,261.3C1200,267,1320,213,1380,186.7L1440,160L1440,320L1380,320C1320,320,1200,320,1080,320C960,320,840,320,720,320C600,320,480,320,360,320C240,320,120,320,60,320L0,320Z"></path></svg>
    </div>

    <div class="footer-wrap">
        <div class="footer-inner"> 
            ApocalypseBlog &copy; 2021<br>
            Powered By Hexo · Theme By <a href="https://github.com/lh1me/hexo-theme-aomori" target="_blank">Aomori</a>
        </div>
    </div>

</footer>




<script src="/dist/build.js?1.14.0.js"></script>


<script src="/dist/custom.js?1.14.0.js"></script>









</body>

</html>